package com.hky.front_end_monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.front_end_monitor.constant.Constants;
import com.hky.front_end_monitor.entity.SysResourceFlow;
import com.hky.front_end_monitor.mapper.SysResourceFlowMapper;
import com.hky.front_end_monitor.service.SysResourceFlowAndWebVitalService;
import com.hky.front_end_monitor.service.SysResourceFlowService;
import com.hky.front_end_monitor.utils.NumberUtils;
import com.hky.front_end_monitor.utils.UuidGen;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author hello
 * @description 针对表【sys_resource_flow】的数据库操作Service实现
 * @createDate 2022-07-28 22:40:40
 */
@Service
public class SysResourceFlowServiceImpl extends ServiceImpl<SysResourceFlowMapper, SysResourceFlow>
        implements SysResourceFlowService {

    @Resource
    SysResourceFlowAndWebVitalService rfAndWebVitalService;

    @Override
    public boolean storeRF(String webVitalUUID, Map map) {
        ArrayList<LinkedHashMap> datas = (ArrayList) (map.get(Constants.metricsName.RF.value));
        String[] uuids = UuidGen.getUUID(datas.size());
        ArrayList<SysResourceFlow> resourceFlows = new ArrayList<>(datas.size());
        for (int i = 0; i < datas.size(); i++) {
            SysResourceFlow rf = new SysResourceFlow();
            LinkedHashMap data = datas.get(i);
            rf.setResourceFlowId(uuids[i]);
            rf.setResourceName((String) data.get("name"));
            rf.setInitiatorType((String) data.get("initiatorType"));
            rf.setTransferSize(NumberUtils.toDouble(data.get("transferSize")));
            rf.setResourceStartTime(NumberUtils.toDouble(data.get("startTime")));
            rf.setResourceResponseEnd(NumberUtils.toDouble(data.get("responseEnd")));
            rf.setDnsLookUp(NumberUtils.toDouble(data.get("dnsLookup")));
            rf.setInitialConnect(NumberUtils.toDouble(data.get("initialConnect")));
            rf.setResourceSsl(NumberUtils.toDouble(data.get("ssl")));
            rf.setResourceRequest(NumberUtils.toDouble(data.get("request")));
            rf.setTtfb(NumberUtils.toDouble(data.get("ttfb")));
            rf.setContentDownload(NumberUtils.toDouble(data.get("contentDownload")));
            resourceFlows.add(rf);
        }
        return this.saveBatch(resourceFlows) &
                rfAndWebVitalService.storeRfAndWebVital(webVitalUUID, uuids);

    }
}




